package cn.consult.sys.service.impl;

import cn.consult.sys.dao.*;
import cn.consult.sys.entity.*;
import cn.consult.sys.service.TApartmentApplyforService;
import cn.consult.sys.service.TCheckEquipmentService;
import cn.consult.sys.service.TDwellEmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * 入住员工管理表 (TDwellEmployee)表服务实现类
 *
 * @author makejava
 * @since 2024-04-08 08:31:55
 */
@Service("tDwellEmployeeService")
public class TDwellEmployeeServiceImpl extends ServiceImpl<TDwellEmployeeDao, TDwellEmployee> implements TDwellEmployeeService {

    @Autowired
    TDwellEmployeeDao tDwellEmployeeDao;
    @Autowired
    TPlaceBuildingDao tPlaceBuildingDao;
    @Autowired
    TPlaceFloorDao tPlaceFloorDao;
    @Autowired
    TPlaceRoomDao tPlaceRoomDao;
    @Autowired
    TApartmentApplyforDao tApartmentApplyforDao;


    @Override
    public PageInfo<TDwellEmployee> getEmployeeAll(Map<String, Object> params) {
        int pageNum = Integer.parseInt(params.get("pageNum").toString());
        int pageSize = Integer.parseInt(params.get("pageSize").toString());
        PageHelper.startPage(pageNum, pageSize, "id desc");
        TDwellEmployee tDwellEmployee = new TDwellEmployee();
        if (params.get("enterpriseName") != null) {
            tDwellEmployee.setEnterpriseName(params.get("enterpriseName").toString());
        }
        if (params.get("enterpriseId") != null) {
            tDwellEmployee.setEnterpriseId(Long.valueOf(params.get("enterpriseId").toString()));
        }
        if (params.get("employeePhone") != null) {
            tDwellEmployee.setEmployeePhone(params.get("employeePhone").toString());
        }

        List<TDwellEmployee> tDwellEmployees = tDwellEmployeeDao.selectByObj(tDwellEmployee);
        PageInfo<TDwellEmployee> appInfoPageInfo = new PageInfo<>(tDwellEmployees);
        return appInfoPageInfo;
    }

    @Override
    public boolean saveEmployee(TDwellEmployee tDwellEmployee) {
        LocalDate now = LocalDate.now();
        tDwellEmployee.setCreateTime(now.toString());
        tDwellEmployee.setCheckInTime(now.toString());
        tDwellEmployee.setLeaseStatus(String.valueOf(1));


        TPlaceBuilding tPlaceBuilding = tPlaceBuildingDao.selectById(tDwellEmployee.getBulidingId());
        TPlaceFloor tPlaceFloor = tPlaceFloorDao.selectById(tDwellEmployee.getFloorId());
        TPlaceRoom tPlaceRoom = tPlaceRoomDao.selectById(tDwellEmployee.getRoomId());


        tDwellEmployee.setObjName(tPlaceBuilding.getBuildingName() + "-" + tPlaceFloor.getFloorName() + "-" + tPlaceRoom.getRoomName());


        return tDwellEmployeeDao.insert(tDwellEmployee) > 0 ? true : false;
    }

    @Override
    public boolean updateEmployee(TDwellEmployee tDwellEmployee) {

        String address = null;
        if (tDwellEmployee.getBulidingId() != null) {
            TPlaceBuilding tPlaceBuilding = tPlaceBuildingDao.selectById(tDwellEmployee.getBulidingId());
            address = tPlaceBuilding.getBuildingName() + "-";
        }
        if (tDwellEmployee.getFloorId() != null) {
            TPlaceFloor tPlaceFloor = tPlaceFloorDao.selectById(tDwellEmployee.getFloorId());
            address = address + tPlaceFloor.getFloorName() + "-";
        }
        if (tDwellEmployee.getRoomId() != null) {
            TPlaceRoom tPlaceRoom = tPlaceRoomDao.selectById(tDwellEmployee.getRoomId());
            address = address + tPlaceRoom.getRoomName();
        }

        tDwellEmployee.setObjName(address);


        return tDwellEmployeeDao.updateById(tDwellEmployee) > 0 ? true : false;
    }

    @Override
    public TDwellEmployee getByIdId(Long id) {
        return tDwellEmployeeDao.getById(id);
    }

    @Override
    public boolean shengqing(Long id) {
        TDwellEmployee tDwellEmployee = tDwellEmployeeDao.getById(id);

        TApartmentApplyfor tApartmentApplyfor = new TApartmentApplyfor();
        tApartmentApplyfor.setApplyforName(tDwellEmployee.getEmployeeName());
        tApartmentApplyfor.setApplyforLocation(tDwellEmployee.getObjName());
        tApartmentApplyfor.setCreateTime(LocalDate.now().toString());
        tApartmentApplyfor.setApplyforPhone(tDwellEmployee.getEmployeePhone());
        tApartmentApplyfor.setBulidingId(tDwellEmployee.getBulidingId());
        tApartmentApplyfor.setFloorId(tDwellEmployee.getFloorId());
        tApartmentApplyfor.setRoomId(tDwellEmployee.getRoomId());
        tApartmentApplyfor.setApplyforStatus(String.valueOf(1));
        tApartmentApplyfor.setApplyforCheckinTime(tDwellEmployee.getCheckInTime());
        tApartmentApplyfor.setApplyforCheckinTime(tDwellEmployee.getTenancyTerm());
        tApartmentApplyfor.setJournalNumber(String.valueOf(tDwellEmployee.getId()));
        int insert = tApartmentApplyforDao.insert(tApartmentApplyfor);


        return insert > 0 ? true : false;
    }
}

